import {
  LOGIN_START,
  LOGIN_SUCCESS,
  LOGIN_FAIL,
  LOGOUT_SUCCESS
} from './actionType'
import { login, logout } from '../requests/user'

//登录开始
export const loginStart = () => {
  return {
    type: LOGIN_START
  }
}
//登录成功
export const loginSuccess = data => {
  return {
    type: LOGIN_SUCCESS,
    payload: { data }
  }
}
//注销成功
export const logoutSuccess = data => {
  return {
    type: LOGOUT_SUCCESS,
    payload: { data }
  }
}
//登录失败
export const loginFail = data => {
  return {
    type: LOGIN_FAIL,
    payload: { data }
  }
}
//用户登录
export const loginStatus = params => {
  return dispatch => {
    dispatch(loginStart())
    login(params).then(resp => {
      if (resp.data.data.status === 1) {
        dispatch(loginSuccess(resp.data.data))
      } else {
        dispatch(loginFail(resp.data.data))
      }
    })
  }
}

//用户注销
export const logoutStatus = userId => {
  return dispatch => {
    dispatch(loginStart())
    logout(userId).then(resp => {
      console.log(resp)
      if (resp.data.res_code === 200) {
        dispatch(logoutSuccess(resp.data.data))
      } else {
        dispatch(loginFail())
      }
    })
  }
}
